import 'package:flutter/material.dart';
import 'package:ft_reader/beans/book_bean.dart';
import 'package:ft_reader/blocs/book_shelf_bloc.dart';
import 'package:ft_reader/common/utils/constans.dart';
import 'package:ft_reader/widgets/book_shelf_item.dart';

class BookShelf extends StatefulWidget {
  @override
  _BookShelf createState()  => _BookShelf();

}

class _BookShelf extends State<BookShelf>
  with AutomaticKeepAliveClientMixin {

  BookShelfBloc _bookBloc = BookShelfBloc();

  bool _isInit = true;

  List<BookBean> _books = List();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _isInit = true;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Column(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(top: 40, left: 20, right: 10),
            height: kToolbarHeight + 20,
            alignment: Alignment.centerLeft,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  '书架',
                  style: TextStyle(
                    color: Theme.of(context).primaryColor,
                    fontSize: 26,
                    fontWeight: FontWeight.bold,
                    letterSpacing: 4,
                  ),
                ),
                IconButton(
                  splashColor: Colors.grey,
                  icon: Icon(
                    MyIcon.searchIcon,
                    color: Theme.of(context).primaryColor,
                    size: 30,
                  ),
                  onPressed: () {
                    print('跳转搜索页面。。。');
                  },
                ),
              ],
            ),
          ),
          Expanded(child: Container(
            margin: EdgeInsets.only(left: 10, right: 10),
            child: RefreshIndicator(
              displacement: 20,
              color: Theme.of(context).primaryColor,
              child: _books.length > 0 ?
              ListView.separated(
                  itemBuilder: (BuildContext context, int index) {
                    return bookShelfItem(context,
                        _books[index], showAlertDartDialog);
                  },
                  separatorBuilder: (BuildContext context, int index) {
                    return Divider(
                      height: 2,
                      color: Theme.of(context).primaryColor,
                    );
                  },
                  itemCount: _books.length) :
              Center(
                child: Text(
                  '这个人贼懒',
                  style: TextStyle(
                    color: Theme.of(context).primaryColor,
                    fontWeight: FontWeight.w200,
                    fontSize: 18.0
                  ),
                ),
              ),
              onRefresh: _onRefresh,
            )
          )),
        ],
      ),
    );
  }

  void showAlertDartDialog(BookBean book) {
    showDialog(context: context,
      builder: (_) => new AlertDialog(
        title: Text('提示'),
        content: Text('是否删除${book.name}'),
        actions: <Widget>[
          FlatButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            child: Text('返回'),
          ),
          FlatButton(
            onPressed: () {
              _isInit = true;
              Navigator.of(context).pop();
            },
            child: Text('确定'),
          )
        ],
      ));
  }

  Future<void> _onRefresh() async {}

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

}